<?php
namespace Home\Controller;

use Home\Event\JssdkEvent;
use Think\Controller;
use Home\Model\B2cMemberAddrsModel;

class AddressController extends BaseController
{
    // 默认 会员ID
    public $member_id;
    public $obj;
    public function __construct(){
        parent::__construct();
        $this->obj = new B2cMemberAddrsModel();
        $this->member_id = $this->member_data['member_id'];//session('userInfo')['member_id'];
        if(!$this->member_id){
            $this->redirect('Login/index');
        }
    }

    /**
     * 收货地址
     */
    public function index()
    {

        $addR = M('b2c_member_addrs')->where(array('member_id'=>$this->member_id))->order('addr_id asc')->field('area,addr,mobile,name,def_addr,addr_id')->select();

        foreach ($addR as $k=>$item){
            $att = str_replace("/","",$item['area']);
            $pos = strpos($att,":");
            if($pos){
                $at = explode(':',$att);
                $addR[$k]['area'] = $at[1];
            }else{
                $addR[$k]['area'] = $att;
            }
        }

        $this->assign('addList',$addR);
        $this->display('Address/index');
    }

    /**
     * 编辑
     */
    public function addAddress(){
        $ak = C('BAIDU_AK');
        $addrId = I('addrId',0,'intval');
        if(!$addrId){
            $this->error('参数错误');
        }

        $data = $this->obj->where(['addr_id' => $addrId])->find();
        //echo $this->obj->_sql();
        $this->assign('data',$data);
        $this->assign('ak',$ak);
        $this->assign('addrId',$addrId);

        $this->assign('url',I('url'));
        $this->display('Address/addressModify');
    }
    public function doAddAddress(){

        $addrId = I('addrId',0,'intval');

        $url = I('url') == 'Member' ? 'Member/memberAddress' : 'Address/index';
        $save['name'] = I('name','','strip_tags');
        if(!$save['name']){
            $this->ajaxReturn(['msg'=>'请输入昵称','status'=>false]);
        }
        $save['mobile'] = I('mobile','','strip_tags');
        if(!$save['mobile']){
            $this->ajaxReturn(['msg'=>'请输入联系电话','status'=>false]);
        }
        if(!isMobile($save['mobile'])){
            $this->ajaxReturn(['msg'=>'请正确输入11位手机号码','status'=>false]);
        }

        $save['area'] = I('area','','strip_tags');
        if(!$save['area']){
            $this->ajaxReturn(['msg'=>'请选择地址','status'=>false]);
        }
        $save['addr'] = I('addr','','strip_tags');
        if(!$save['addr']){
            $this->ajaxReturn(['msg'=>'请输入详细地址','status'=>false]);
        }
        $save['store_lnt'] = I('store_lnt');
        $save['store_lat'] = I('store_lat');
        $save['def_addr'] = I('def_addr',0,'intval');
        /*dump($addrId);
        dump($save);die;*/
        //设置默认地址
        if($save['def_addr']){
            $this->obj->where(['member_id' => $this->member_id])->save(['def_addr' => 0]);
        }

        if($addrId){
            $stat = $this->obj->where(['addr_id' => $addrId])->save($save);
        }else{
            $save['member_id'] = $this->member_id;
            $stat = $this->obj->add($save);
        }
        if($stat !== false){

            if(I('def_addr',0,'intval') == 1){
                $url = 'Carts/cart';
            }

            $this->ajaxReturn(['msg'=>'编辑成功','status'=>true,'url'=>url($url)]);
        }else{
            $this->ajaxReturn(['msg'=>'编辑失败','status'=>false]);
        }
        //$stat === false ? $this->error('编辑失败') : $this->success('编辑成功',url($url));
    }

    /**
     * 删除地址
     */
    public function deleteAddr(){
        $addrId = I('addrId',0,'intval');
        if(!$addrId){
            $this->error('参数错误');
        }
        $stat = $this->obj->where(['addr_id' => $addrId])->delete();
        //$stat === false ? $this->error('删除失败') : $this->success('删除成功',U('index'));
        if($stat !== false){
            $this->ajaxReturn(['msg'=>'删除成功','status'=>true]);
        }else{
            $this->ajaxReturn(['msg'=>'删除失败','status'=>false]);
        }

    }

    /**
     * 选择收货地址
     */
    public function changeAddress(){
        $addrId = I('addr',0) or $this->ajaxReturn(['msg'=>'参数错误','status'=>0]);
        $result = $this->obj->changeAddress($this->member_id,$addrId);
        $this->ajaxReturn($result);
    }
    /**
     * 新增收货地址
     */
    public function addressModify(){
        //dump(I('url'));die;
        $ak = C('BAIDU_AK');
        //$jssdk = new JssdkEvent();
        //$signPackage = $jssdk->GetSignPackage();
        //$this->assign(['signPackage' => $signPackage]);
        $this->assign('ak',$ak);
        $this->assign('url',I('url'));
        $this->display();
    }


    /**
     * 自提点
     */
    public function addressSince(){
        $local_id = I('get.sid');
        $Zti = M('b2c_carts')->where(['member_id'=>$this->member_id,'local_id'=>$local_id])->select();

        //用户选过自提点
        $C_is = false;
        $data = array();
        foreach ($Zti as $k=>$value){
            if(!empty($value['dot_id'])&& !$C_is){
                $data['dot_id'] = $value['dot_id'];
                $data['mobile'] = $value['mobile'];
                $data['local_id'] = $local_id;
                $C_is = true;
            }
        }
        //未选过自提点，电话号码默认收货地址电话
        if(empty($data)){
            $sMobile= M('b2c_member_addrs')->where(array('member_id'=>$this->member_id,'def_addr'=>1))->field('mobile')->find();
            $data['mobile'] = $sMobile['mobile'];
            $data['local_id'] = $local_id;
        }
        //自提点列表
        $ztList = M('b2c_store_dot')->where(['local_id'=>$local_id])->select();

        $this->assign('data',$data);
        $this->assign('zTList',$ztList);
        $this->display('Address/addressSince');
    }

    public function __destrcut(){
        parent::__destruct();
        unset($this->obj);
        unset($this->member_id);
    }


}